package com.gxuwz.prrs.activity;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import com.gxuwz.prrs.R;
import com.gxuwz.prrs.bean.ChangePasswordForm;
import com.gxuwz.prrs.service.ServiceHolder;
import com.gxuwz.prrs.service.impl.AuthServiceImpl;
import com.gxuwz.prrs.utils.NavigationUtils;

import java.util.Objects;

/**
 * @author 张钧
 * 修改密码页面
 */
public class ChangePasswordActivity extends AppCompatActivity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_chang_password);
		NavigationUtils.setRedirectBottom(this);
		TextView oldPassword = findViewById(R.id.old_password);
		TextView newPassword = findViewById(R.id.new_password);
		TextView newPasswordAgain = findViewById(R.id.new_password_again);
		TextView tip = findViewById(R.id.tip);
		Button submit = findViewById(R.id.submit);
		AuthServiceImpl authService = ServiceHolder.getInstance().getBean(AuthServiceImpl.class);
		submit.setOnClickListener(view -> {
			String a = newPassword.getText().toString();
			String b1 = newPasswordAgain.getText().toString();
			if(!Objects.equals(a, b1)){
				tip.setText("两次输入新密码不一致");
			}else {
				boolean b = authService.changePassword(new ChangePasswordForm().setNewPassword(newPassword.getText().toString()).setOldPassword(oldPassword.getText().toString()));
				if(b){
					SharedPreferences sharedPreferences = ChangePasswordActivity.this.getSharedPreferences("token", Context.MODE_PRIVATE);
					SharedPreferences.Editor editor = sharedPreferences.edit().remove("token");
					editor.apply();
					Toast.makeText(this, "修改成功请重新登录", Toast.LENGTH_SHORT).show();
					NavigationUtils.redirectToLoginActivity(this,null);
				}else {
					tip.setText("旧密码错误，请重新输入");
				}
			}
		});
	}
}
